package com.central.apps.handler.project;

import com.alibaba.fastjson.JSONArray;
import com.central.app.common.base.BaseHandler;
import com.central.app.service.HandlerService;
import com.central.app.service.TreeService;
import com.central.apps.common.constant.BizConstant;
import com.central.apps.service.BudgetService;
import com.central.bizcommon.model.flow.FlowNode;
import com.central.bizcommon.model.flow.FlowOpinion;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 处理收支填报业务逻辑
 *
 * @author lidachao
 * @date 2022/9/28
 */
@Component
@Slf4j
public class ProjectCostFillHandler extends BaseHandler {

    @Autowired
    private HandlerService handlerService;
    @Autowired
    private BudgetService budgetService;
    @Autowired
    private TreeService treeService;

    @Override
    public String beforeExcuteFlow(String app, Long id, FlowNode flowNode, FlowOpinion op) throws Exception {
        if("submit".equals(flowNode.getId())) {
            // 查询成本单明细信息
            List<Map<String, Object>> pcflList = handlerService.findBySql("select * from gq_procostfillline where pcfid = " + id);

            if (CollectionUtils.isEmpty(pcflList)) {
                return "成本单明细不能为空";
            }
        }

        if("complete".equals(flowNode.getId()) && op.isFlag()) {
            // 查询成本单明细信息
            Map<String, Object> map = op.getVariables();
            Integer proId = MapUtils.getInteger(map, "proid");
            Integer pbdId = MapUtils.getInteger(map, "pbdid");
            String totalCost = MapUtils.getString(map, "totalcost");

            // 更新年度预算实际使用金额
            budgetService.updateBudgetInfo(proId, pbdId, new BigDecimal(totalCost));
        }

        return null;
    }

    @Override
    public Object operate(String app, String type, Map<String, Object> params) throws Exception {
        if ("getYear".equals(type)) {
            String subjectNo = MapUtils.getString(params, "subjectno");
            String tableName = "gq_proplan";

            if (BizConstant.PUBLIC_SUBJECT_NO.equals(subjectNo)) {
                tableName = "gq_propublicbudget";
            }

            List<Map<String, Object>> list = handlerService.findListByObj(tableName, params);

            if (CollectionUtils.isNotEmpty(list)) {
                JSONObject json = new JSONObject();
                json.put("code", "0");
                json.put("count", list.size());
                json.put("data", JSONArray.toJSONString(list));
                return json.toString();
            }
        }
        if ("getBudget".equals(type)) {
            String subjectNo = MapUtils.getString(params, "subjectno");

            if (BizConstant.PUBLIC_SUBJECT_NO.equals(subjectNo)) {
                app = "propublicbudgetApp";
                params.put("object", "gq_ppbudget");
                params.put("pbid", params.get("id"));
                params.put("id_orders", "asc");
            } else {
                app = "proplanApp";
                params.put("object", "gq_probudget");
                params.put("ppid", params.get("id"));
                params.put("sortno_orders", "asc");
            }
            params.remove("id");

            List<Map<String, Object>> list = treeService.findTreeV1(app, params, false);

            if (CollectionUtils.isNotEmpty(list)) {
                JSONObject json = new JSONObject();
                json.put("code", "0");
                json.put("count", list.size());
                json.put("data", list);
                return json.toString();
            }
        }
        return null;
    }
}
